package org.hu.controller;


import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;

import lombok.extern.slf4j.Slf4j;
import org.hu.entity.pojo.Order;
import org.hu.entity.pojo.OrderDetail;
import org.hu.entity.vo.Result;
import org.hu.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@Slf4j
@Data
@RestController
public class WxPayController {


    private OrderMapper orderMapper;
    public WxPayController(OrderMapper orderMapper){
        this.orderMapper = orderMapper;
    }

    @Value("${wxpay.app-id}")
    private String appId;

    @Value("${wxpay.mch-id}")
    private String mchId;

    @Value("${wxpay.api-v3-key}")
    private String apiV3Key;

    @Value("${wxpay.notify-url}")
    private String notifyUrl;



    @PostMapping("/createOrder")
    public Result<Map<String, Object>> createOrder(@RequestBody Order order) throws Exception {

        // 生成订单号
        String outTradeNo = UUID.randomUUID().toString().replace("-", "");

        ObjectMapper mapper = new ObjectMapper();
        ObjectNode orderMap = mapper.createObjectNode();
        // 构造下单请求
//        JSONObject amount = new JSONObject();

        orderMap.put("id",order.getId());
        orderMap.put("amount", order.getAmount());
        orderMap.put("orderTime", String.valueOf(order.getOrderTime()));
        ObjectNode payer = new ObjectMapper().createObjectNode();
//        payer.put("openid", order.getOpenid());
        ObjectNode reqData = new ObjectMapper().createObjectNode();
        reqData.put("appid", appId);
        reqData.put("mchid", mchId);
        reqData.put("description", "商品描述");
        reqData.put("out_trade_no", outTradeNo);
        reqData.put("notify_url", notifyUrl);
        reqData.put("order", orderMap);
        reqData.put("payer", payer);
        // 调用微信支付下单接口（使用你引入的SDK）
        // 返回 prepay_id 等支付参数给前端
        // 以下是伪代码，需结合 SDK 正确实现
        Map<String, Object> payParams = new HashMap<>();
        payParams.put("timeStamp", "xxx");
        payParams.put("nonceStr", "xxx");
        payParams.put("package", "prepay_id=xxx");
        payParams.put("signType", "RSA");
        payParams.put("paySign", "xxx");
        payParams.put("reqData", reqData);
        return Result.success("订单创建成功",payParams);
    }

    @PostMapping("/notify")
    public Result<String> payNotify(HttpServletRequest request,@RequestBody Order order) {
        // 读取微信服务器的回调数据
        // 验签并更新订单状态
         log.info("更新订单状态");
         // 修改订单状态
        orderMapper.updatePayStatus(order.getId());
        // 修改对应的订单细则支付状态
        OrderDetail orderDetail =  new OrderDetail();
        orderDetail.setOrderId(order.getId());
        orderDetail.setPayStatus(2);
        orderMapper.updateOrderDetailById(orderDetail);
        return Result.success("支付成功");
    }

}

